
# Begin:@Package@::@Class@

class @Class@(genpy.Message):
  _md5sum = "@Checksum@"
  _type = "@Package@/@Class@"
  _has_header = False
  _full_text = "Protobuf generated msg for @Package@::@Class@"
  __slots__ = ['pb']
  _slot_types = ['@Package@/@PROTO_PY@']

  def __init__(self, *args, **kwds):
    if args or kwds:
      super(@Class@, self).__init__(*args, **kwds)
      if self.pb is None:
        self.pb = @PROTO_PY@.@Class@()
    else:
      self.pb = @PROTO_PY@.@Class@()

  def _get_types(self):
    return self._slot_types

  @staticmethod
  def fill_protobuf_from_dict(proto_obj, dict_data):
      if not proto_obj:
          raise ValueError("Invalid Protobuf object")
      
      proto_class = proto_obj.__class__

      for field in proto_class.DESCRIPTOR.fields:
          field_name = field.name
          if field_name not in dict_data:
              continue

          if field.label == _descriptor.FieldDescriptor.LABEL_REPEATED:
              field_value = dict_data.get(field_name, [])
              for item in field_value:
                  if field.type == _descriptor.FieldDescriptor.TYPE_MESSAGE:
                      sub_proto_class = field.message_type._concrete_class
                      item_obj = sub_proto_class()
                      @Class@.fill_protobuf_from_dict(item_obj, item)
                      getattr(proto_obj, field_name).append(item_obj)
                  else:
                      getattr(proto_obj, field_name).append(item)
          elif field.type == _descriptor.FieldDescriptor.TYPE_MESSAGE:
              field_value = dict_data[field_name]
              sub_proto_obj = getattr(proto_obj, field_name)
              @Class@.fill_protobuf_from_dict(sub_proto_obj, field_value)
          else:
              field_value = dict_data.get(field_name)
              if field.type == FieldDescriptor.TYPE_STRING:
                field_value = str(field_value)
              setattr(proto_obj, field_name, field_value)

  def serialize(self, buff):
    try:
      if isinstance(self.pb, dict):
          obj = @Class@_pb2.@Class@()
          @Class@.fill_protobuf_from_dict(obj, self.pb)
          self.pb = obj
      
      buff.write(self.pb.SerializeToString())
    except struct.error as se:
      self._check_types(
          struct.error("%s: '%s' when writing '%s'" % (type(se), str(se), str(locals().get(
              '_x', self)))))
    except TypeError as te:
      self._check_types(
          ValueError("%s: '%s' when writing '%s'" % (type(te), str(te), str(locals().get(
              '_x', self)))))

  def deserialize(self, str):
    try:
      self.pb = @PROTO_PY@.@Class@()
      self.pb.ParseFromString(str)
      return self
    except struct.error as e:
      raise genpy.DeserializationError(e)

  def serialize_numpy(self, buff, numpy):
    self.serialize(buff)

  def deserialize_numpy(self, str, numpy):
    self.deserialize(str)
    return self

# End:@Package@::@Class@
